<?php

//栏位模块
class Position_model extends Model {

	function Position_model()
	{
		parent::Model();	
		$this->load->database();
	}
	
	//获取列表
	function list_item($offset = 0, $pagecount = 20, $statictype = "all", $order = "id", $desc = "desc")
	{
		$items = array();
		$this->db->from("position");
		if(in_array($statictype, array(1,2))) {//条件搜索
			$this->db->where("statictype",$statictype);
		}
		$query = $this->db->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
			foreach ($items as $key => $item){
				$item['statictype'] == '1' ? $items[$key]["type"] = "热闹" : $items[$key]["type"] = "主题";
			}
		}
		return $items;
	}
	
	//计算总数
	function count_items()
	{
		return $this->db->count_all_results("position");
	}
	
	//获取
	function get($id)
	{
		$q = $this->db->get_where("position", array("id"=>$id));
		$row = $q->row_array();
		return $row;
	}
	
	//添加
	function add($data, $imageurl = 'background')
	{
		//处理图片
		$background = ciupload($imageurl);
		//等比裁剪
		if($background != "") {
			$params = array('src_file' => './data/upload_pic/'.$background, 'dst_file' => './data/upload_pic/'.$background);
			//$this->load->library("imagecrop", $params);
			$this->load->library("imagecrop");
			$this->imagecrop->src_file=$params['src_file'];
			$this->imagecrop->dst_file=$params['dst_file'];
			$this->imagecrop->LoadImage();
			$this->imagecrop->Crop(220,165,1);
			$this->imagecrop->SaveImage();
			$this->imagecrop->destory();
		}
		$data["background"] = $background;
		
		//execute sql
		$result = $this->db->insert("position", $data);
		return $result;
	}
	
	//编辑
	function update($id, $data, $imageurl = 'background')
	{
		//处理图片
		$background = ciupload($imageurl);
		//等比裁剪
		if($background != "") {
			$params = array('src_file' => './data/upload_pic/'.$background, 'dst_file' => './data/upload_pic/'.$background);
			//$this->load->library("imagecrop", $params);
			$this->load->library("imagecrop");
			$this->imagecrop->src_file=$params['src_file'];
			$this->imagecrop->dst_file=$params['dst_file'];
			$this->imagecrop->LoadImage();
			$this->imagecrop->Crop(220,165,1);
			$this->imagecrop->SaveImage();
			$this->imagecrop->destory();
			$data["background"] = $background;
		}
		//execute sql
		$this->db->where("id", $id);
		$result = $this->db->update("position", $data);
		return $result;
	}
	
	//删除选择
	function delselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//execute sql
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("position");
		return $result;
	}
	
}

/* End of file */